package com.example.musicapp.ui.playlist

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.musicapp.MainActivity
import com.example.musicapp.R
import com.example.musicapp.databinding.FragmentPlaylistBinding
import com.example.musicapp.model.Song
import com.example.musicapp.network.RetrofitClient
import com.example.musicapp.viewmodel.MusicViewModel
import kotlinx.coroutines.launch

class PlaylistFragment : Fragment() {
    private var _binding: FragmentPlaylistBinding? = null
    private val binding get() = _binding!!
    private val viewModel: MusicViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentPlaylistBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        fetchPlaylist()
    }

    private fun fetchPlaylist() {
        viewLifecycleOwner.lifecycleScope.launch {
            try {
                val playlistResponses = RetrofitClient.musicApi.getPlaylist()
                android.util.Log.d("PlaylistFragment", "Playlist received: $playlistResponses")
                
                val songs = playlistResponses.map { response ->
                    Song(
                        id = response.id,
                        title = response.title,
                        artist = response.singer,
                        album = "",
                        coverUrl = response.coverImgUrl,
                        duration = when(response.id) {
                            1 -> 253  // 甜蜜蜜
                            2 -> 224  // 小城故事
                            3 -> 271  // 雨蝶
                            4 -> 242  // 风中的承诺
                            else -> response.duration ?: 180
                        }
                    )
                }
                setupRecyclerView(songs)
            } catch (e: Exception) {
                android.util.Log.e("PlaylistFragment", "Error fetching playlist", e)
                e.printStackTrace()
            }
        }
    }

    private fun setupRecyclerView(songs: List<Song>) {
        viewModel.setPlaylist(songs)
        
        binding.songsRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = SongAdapter(songs) { song ->
                viewModel.playSong(song)
                (activity as? MainActivity)?.switchToPage(1)
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 